package com.fan.shop.businesslogic;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.fan.shop.domain.StockReq;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.model.userclass.StockReqLineUserClass;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.ItemService;
import com.fan.shop.services.StockReqLineService;
import com.fan.shop.services.StockReqService;
import com.fan.shop.services.StockService;
import com.fan.shop.services.StoreService;

public class StockReqFinaliseProcess extends ProcExec<Integer> {

	private static Logger logger = Logger
			.getLogger(StockReqFinaliseProcess.class);
	private String requisitionNo;
	private BigDecimal postage;
	private String comment;

	private StockReqService stockReqService;
	private StockReqLineService stockReqLineService;

	private StockService stockService;
	private ItemService itemService;
	private StoreService storeService;

	private List<StockReqLineUserClass> attachedLines;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		StockReqFinaliseProcess.logger = logger;
	}

	public String getRequisitionNo() {
		return requisitionNo;
	}

	public void setRequisitionNo(String requisitionNo) {
		this.requisitionNo = requisitionNo;
	}

	public BigDecimal getPostage() {
		return postage;
	}

	public void setPostage(BigDecimal postage) {
		this.postage = postage;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public StockReqService getStockReqService() {
		return stockReqService;
	}

	public void setStockReqService(StockReqService stockReqService) {
		this.stockReqService = stockReqService;
	}

	public StockReqLineService getStockReqLineService() {
		return stockReqLineService;
	}

	public void setStockReqLineService(StockReqLineService stockReqLineService) {
		this.stockReqLineService = stockReqLineService;
	}

	public List<StockReqLineUserClass> getAttachedLines() {
		return attachedLines;
	}

	public void setAttachedLines(List<StockReqLineUserClass> attachedLines) {
		this.attachedLines = attachedLines;
	}

	public StockService getStockService() {
		return stockService;
	}

	public void setStockService(StockService stockService) {
		this.stockService = stockService;
	}

	public ItemService getItemService() {
		return itemService;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public StoreService getStoreService() {
		return storeService;
	}

	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}

	@Override
	public Integer processImpl() {
		if (!stockReqService.hasStockReq(requisitionNo)) {
			setErrorMessage("Stock Req: " + requisitionNo + " doesn't exist!");
			return ShopGlobalVariables.FAILED;
		}

		StockReq stockReq = stockReqService.getStockReq(requisitionNo);
		stockReq.setStatus(ShopGlobalVariables.RECEIVED);
		stockReq.setPostage(postage);
		stockReq.setComment(comment);
		stockReq.setReceivedDate(new Date(System.currentTimeMillis()));

		for (StockReqLineUserClass lineUserClass : attachedLines) {
			StockReqLineFinaliseProcess stockReqLineFinaliseProcess = new StockReqLineFinaliseProcess();
			stockReqLineFinaliseProcess.setRequisitionNo(requisitionNo);
			stockReqLineFinaliseProcess.setItemCode(lineUserClass.getItemCode());
			stockReqLineFinaliseProcess
					.setStockReqLineService(stockReqLineService);
			int result = stockReqLineFinaliseProcess.process();
			if (result == ShopGlobalVariables.FAILED) {
				setErrorMessage(stockReqLineFinaliseProcess.getErrorMessage());
				return ShopGlobalVariables.FAILED;
			}
		}
		stockReqService.updateStockReq(stockReq);

		StockReqAddToStockProcess stockReqAddToStockProcess = new StockReqAddToStockProcess();
		stockReqAddToStockProcess.setItemService(itemService);
		stockReqAddToStockProcess.setStockReqLineService(stockReqLineService);
		stockReqAddToStockProcess.setStockReqService(stockReqService);
		stockReqAddToStockProcess.setStockService(stockService);
		stockReqAddToStockProcess.setStoreService(storeService);

		stockReqAddToStockProcess.setStockReq(stockReq);

		int result = stockReqAddToStockProcess.process();

		if (result == ShopGlobalVariables.FAILED) {
			setErrorMessage(stockReqAddToStockProcess.getErrorMessage());
			return result;
		} else {
			return ShopGlobalVariables.SUCCESS;
		}
	}

}
